#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
    pid_t pidA, pidB, pidC, pidD;

    // 创建子进程A
    pidA = fork();
    if (pidA < 0) {
        perror("fork error in creating process A");
        exit(EXIT_FAILURE);
    } else if (pidA == 0) {  // 子进程A
        time_t now;
        struct tm *time_info;
        time(&now);
        time_info = localtime(&now);
        char time_str[80];
        strftime(time_str, 80, "Current time is <%Y %m %d %H %M %S>", time_info);
        printf("Process A: %s\n", time_str);
        printf("Process A: PID = %d, Parent PID = %d\n", getpid(), getppid());
        exit(EXIT_SUCCESS);
    }

    // 创建子进程B
    pidB = fork();
    if (pidB < 0) {
        perror("fork error in creating process B");
        exit(EXIT_FAILURE);
    } else if (pidB == 0) {  // 子进程B
        printf("Process B: I am a younger brother process\n");
        printf("Process B: PID = %d, Parent PID = %d\n", getpid(), getppid());

        // 创建子进程B1
        pidC = fork();
        if (pidC < 0) {
            perror("fork error in creating process B1");
            exit(EXIT_FAILURE);
        } else if (pidC == 0) {  // 子进程B1
            printf("Process B1: My student ID is <Your Student ID>\n");
            printf("Process B1: PID = %d, Parent PID = %d\n", getpid(), getppid());
            exit(EXIT_SUCCESS);
        }

        // 创建子进程B2
        pidD = fork();
        if (pidD < 0) {
            perror("fork error in creating process B2");
            exit(EXIT_FAILURE);
        } else if (pidD == 0) {  // 子进程B2
            printf("Process B2: My name is <Your Name>\n");
            printf("Process B2: PID = %d, Parent PID = %d\n", getpid(), getppid());
            exit(EXIT_SUCCESS);
        }

        // 等待子进程B1和B2结束
        waitpid(pidC, NULL, 0);
        waitpid(pidD, NULL, 0);
        exit(EXIT_SUCCESS);
    }

    // 父进程等待子进程A和B结束
    waitpid(pidA, NULL, 0);
    waitpid(pidB, NULL, 0);
    printf("Parent process: I am the father process\n");
    printf("Parent process: PID = %d, Parent PID = %d\n", getpid(), getppid());
    return EXIT_SUCCESS;
}